package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxt
 * @create 2025-07-16-11:48
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;

    /**
     * 分页查询供应商
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        HashMap<String, Object> result = new HashMap<>();
        int start = (page - 1) * rows;
        List<Supplier> list = supplierDao.selectListPage(start, rows, supplierName);
        Integer count = supplierDao.selectCount();
        result.put("total", count);
        result.put("rows", list);
        return result;
    }

    /**
     * 新增或修改供应商
     * @param supplierId
     * @param supplier
     */
    @Override
    public void saveOrUpdate(Integer supplierId, Supplier supplier) {
        if (supplierId == null){
            supplierDao.insert(supplier);
        }else {
            supplierDao.update(supplierId, supplier);
        }
    }

    /**
     * 删除供应商
     * @param ids
     */
    @Override
    public void delete(String ids) {
        String[] supplierIdList = ids.split(",");
        for (String supplierId : supplierIdList) {
            supplierDao.delete(supplierId);
        }
    }

    /**
     * 下拉列表查询供应商
     * @param q 供应商名称模糊查询
     * @return
     */
    @Override
    public List<Supplier> getComboboxList(String q) {
        List<Supplier> supplierList = supplierDao.getComboboxList(q);
        return supplierList;
    }
}
